<?php

class DocsController extends Zend_Controller_Action
{
    private  $auth;
    public function init()
    {
        $this->auth=  Zend_Auth::getInstance();
    }

    public function indexAction()
    {
       
    }

   public function addAction(){
        $this->view->title="Add Documents - Welcome to Rosemoore Lake";
       try{
            if(isset ($_POST['action'])){
                         if( $_FILES["fileName"]["error"] == UPLOAD_ERR_INI_SIZE){
                              $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Document greater than 2 MB not allowed</font></center>";
                         }else{
                                  $config = Zend_Registry::get('mail_config');

                                 $name_of_uploaded_file =  basename($_FILES['fileName']['name']);

                                  $size_of_uploaded_file = $_FILES["fileName"]["size"]/1024;

                           if($name_of_uploaded_file !="" && $size_of_uploaded_file ==0){
                                 $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>Invalid file format.</font></center>";
                           }else{
                              $path_of_uploaded_file = $config->docs->tempfolder . $name_of_uploaded_file;
                              $tmp_path = $_FILES["fileName"]["tmp_name"];

                              if(is_uploaded_file($tmp_path))
                              {
                                 if(file_exists($path_of_uploaded_file)){
                                     $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Document already exists</font></center>";
                                 }elseif(!copy($tmp_path,$path_of_uploaded_file))
                                  {
                                        $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to upload  the Document</font></center>";
                                  }else{

                                      $documents = new Application_Model_Documents();
                                      $documents->setDocName($_POST['docName']);
                                      $documents->setFileName($name_of_uploaded_file);
                                      $documents->setFileSize($size_of_uploaded_file);
                                      $documents->setCategory($_POST["category"]);
                                      $documents->setCreatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS);
                                      $date = new Zend_Date();
                                      $documents->setCreatedDate($date->getIso());

                                      $documentsMapper = new Application_Model_DocumentsMapper();

                                      $documentsMapper->add($documents);

                                      $this->view->message="<img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> Document added Successfully</font>";
                                  }
                           }
                     }
                 }
            }
        }catch(Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> Technical difficulties expierenced in adding Document</font>";
        }

    }

    public function viewAction(){
         $this->view->title="View  Documents - Welcome to Rosemoore Lake";
         try{
              $documents = new Application_Model_Documents();
              $documentsMapper = new Application_Model_DocumentsMapper(); 

             if(isset ($_POST['action']) && $_POST['action'] == "_search"){

                 $documents->setDocName($_POST['_docName']);
                 $documents->setCategory($_POST['_category']);
    
                 $this->view->docName= $_POST['_docName'];
                 $this->view->category= $_POST['_category'];

             }elseif(isset ($_POST['action']) && $_POST['action'] == "_sort"){
                 $documents->setDocName($_POST['_docName']);
                 $documents->setCategory($_POST['_category']);
                 $documents->setSortBy($_POST['_sortBy']);

                 $this->view->docName= $_POST['_docName'];
                 $this->view->category= $_POST['_category'];

             }
             
             $this->view->searchData = $documentsMapper->get($documents);
             
        }catch(Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> Technical difficulties expierenced in getting  Document</font>";
        }

    }

    public function editAction(){
         $this->view->title="Edit Documents - Welcome to Rosemoore Lake";

    }

    public function deleteAction(){

    }

}

